package cn.iocoder.yudao.module.erp.controller.admin.product;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.erp.controller.admin.product.vo.category.ErpCategorySettingPageReqVO;
import cn.iocoder.yudao.module.erp.controller.admin.product.vo.category.ErpCategorySettingRespVO;
import cn.iocoder.yudao.module.erp.controller.admin.product.vo.category.ErpCategorySettingSaveReqVO;
import cn.iocoder.yudao.module.erp.dal.dataobject.product.ErpCategorySettingDO;
import cn.iocoder.yudao.module.erp.service.product.ErpCategorySettingService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.Collection;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;

@Tag(name = "管理后台 - ERP 类别设定")
@RestController
@RequestMapping("/erp/category-setting")
@Validated
public class ErpCategorySettingController {

    @Resource
    private ErpCategorySettingService categorySettingService;

    @PostMapping("/create")
    @Operation(summary = "创建类别设定")
    @PreAuthorize("@ss.hasPermission('erp:category-setting:create')")
    public CommonResult<Long> createCategorySetting(@Valid @RequestBody ErpCategorySettingSaveReqVO createReqVO) {
        return success(categorySettingService.createCategorySetting(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新类别设定")
    @PreAuthorize("@ss.hasPermission('erp:category-setting:update')")
    public CommonResult<Boolean> updateCategorySetting(@Valid @RequestBody ErpCategorySettingSaveReqVO updateReqVO) {
        categorySettingService.updateCategorySetting(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除类别设定")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('erp:category-setting:delete')")
    public CommonResult<Boolean> deleteCategorySetting(@RequestParam("id") Long id) {
        categorySettingService.deleteCategorySetting(id);
        return success(true);
    }

    @DeleteMapping("/batch-delete")
    @Operation(summary = "批量删除类别设定")
    @Parameter(name = "ids", description = "编号数组", required = true)
    @PreAuthorize("@ss.hasPermission('erp:category-setting:delete')")
    public CommonResult<Boolean> deleteCategorySettings(@RequestParam("ids") Collection<Long> ids) {
        categorySettingService.deleteCategorySettings(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得类别设定")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('erp:category-setting:query')")
    public CommonResult<ErpCategorySettingRespVO> getCategorySetting(@RequestParam("id") Long id) {
        ErpCategorySettingDO categorySetting = categorySettingService.getCategorySetting(id);
        return success(BeanUtils.toBean(categorySetting, ErpCategorySettingRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得类别设定分页")
    @PreAuthorize("@ss.hasPermission('erp:category-setting:query')")
    public CommonResult<PageResult<ErpCategorySettingRespVO>> getCategorySettingPage(@Valid ErpCategorySettingPageReqVO pageReqVO) {
        PageResult<ErpCategorySettingDO> pageResult = categorySettingService.getCategorySettingPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ErpCategorySettingRespVO.class));
    }

    @GetMapping("/list")
    @Operation(summary = "获得类别设定列表")
    @PreAuthorize("@ss.hasPermission('erp:category-setting:query')")
    public CommonResult<List<ErpCategorySettingRespVO>> getCategorySettingList(@Valid ErpCategorySettingPageReqVO listReqVO) {
        List<ErpCategorySettingDO> list = categorySettingService.getCategorySettingList(listReqVO);
        return success(BeanUtils.toBean(list, ErpCategorySettingRespVO.class));
    }

    @GetMapping("/categories")
    @Operation(summary = "获得所有主类别")
    @PreAuthorize("@ss.hasPermission('erp:category-setting:query')")
    public CommonResult<List<ErpCategorySettingRespVO>> getCategories() {
        List<ErpCategorySettingDO> list = categorySettingService.getCategories();
        return success(BeanUtils.toBean(list, ErpCategorySettingRespVO.class));
    }

    @GetMapping("/items")
    @Operation(summary = "根据父级ID获得子项目列表")
    @Parameter(name = "parentId", description = "父级ID", required = true)
    @PreAuthorize("@ss.hasPermission('erp:category-setting:query')")
    public CommonResult<List<ErpCategorySettingRespVO>> getItemsByParentId(@RequestParam("parentId") Long parentId) {
        List<ErpCategorySettingDO> list = categorySettingService.getItemsByParentId(parentId);
        return success(BeanUtils.toBean(list, ErpCategorySettingRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出类别设定 Excel")
    @PreAuthorize("@ss.hasPermission('erp:category-setting:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportCategorySettingExcel(@Valid ErpCategorySettingPageReqVO listReqVO,
              HttpServletResponse response) throws IOException {
        List<ErpCategorySettingDO> list = categorySettingService.getCategorySettingList(listReqVO);
        // 导出 Excel
        ExcelUtils.write(response, "类别设定.xls", "数据", ErpCategorySettingRespVO.class,
                        BeanUtils.toBean(list, ErpCategorySettingRespVO.class));
    }

}




